# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  
  #
  # Avatars
  #
  def avatar_for(email, options = {}) 
    
    if APP_SETTINGS[:use_gravatars]
      
      require 'digest/md5'

      url_for({ :gravatar_id => Digest::MD5.hexdigest(email), :host => 'www.gravatar.com',      
                   :protocol => 'http://', :only_path => false, :controller => 'avatar.php'    
                }.merge(options))  
                
    else
      #TODO
    end
  end  
  
  #
  # Allow time_ago_in_words but with cached pages, do it in JS!
  #
  def cachable_time_ago_in_words(from, id_prefix = 'time_ago')
    id = "#{id_prefix}_#{from.to_i}"
    content_tag(:span, from.to_formatted_s(:long), :id => id) <<
    javascript_tag("$('#{id}').update(DateHelper.timeAgoInWords(#{(from.to_i * 1000).to_json}))")
  end
  
  def snippet(thought, wordcount) 
    thought.split[0..(wordcount-1)].join(" ") + (thought.split.size > wordcount ? "..." : "") 
  end
  
  
end
